package org.jeecgframework.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFamily;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;


public class FileConverter {

	static DefaultDocumentFormatRegistry DOCUMENT_REG = new DefaultDocumentFormatRegistry();
	private static String outputFilePath;
	static{
		DocumentFormat docx = new DocumentFormat("Microsoft Word", DocumentFamily.TEXT, "application/msword", "docx");
		docx.setExportFilter(DocumentFamily.TEXT, "MS Word 2007");
		DOCUMENT_REG.addDocumentFormat(docx);
		
		DocumentFormat pptx = new DocumentFormat("Microsoft PowerPoint", DocumentFamily.PRESENTATION, "application/vnd.ms-powerpoint", "pptx");
		pptx.setExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 2007");
		DOCUMENT_REG.addDocumentFormat(pptx);
		
		DocumentFormat xlsx = new DocumentFormat("Microsoft Excel", DocumentFamily.SPREADSHEET, "application/vnd.ms-excel", "xlsx");
		xlsx.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 2007");
		DOCUMENT_REG.addDocumentFormat(xlsx);
	}
	
	public void pdfGen(String inFilePath,String outFilePath){
		System.out.println("inFilePath:"+inFilePath);
		System.out.println("outFilePath:"+outFilePath);
		Office2Pdf pdf_tool = new Office2Pdf(inFilePath, outFilePath);
		pdf_tool.start();
	}
	
	public void swfGen(String inFilePath,String outFilePath){
		Pdf2Swf swf_tool = new Pdf2Swf("/home/appuser/swftools/src/pdf2swf", inFilePath, outFilePath);
		swf_tool.start();
	}
	
	private class Office2Pdf extends Thread{
		
		private File input_file;
		private File output_file;
		
		public Office2Pdf(String inputFilePath,String outputFilePath) {
			super();
			input_file = new File(inputFilePath);
			output_file = new File(outputFilePath);
		}

		@Override
		public void run() {
			super.run();
			System.out.println("pdf_tool.start();////////////////////***************");
			//创建与OpenOffice的连接
			OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
			System.out.println("连接成功？！");
			try {
				connection.connect();
				DocumentConverter convert = new OpenOfficeDocumentConverter(connection,DOCUMENT_REG);
				convert.convert(input_file, output_file);
				System.out.println("转换PDF文件");
				System.out.println("-------------------------------------");
				System.out.println("原路径："+input_file);
				System.out.println("新路径："+output_file);
				System.out.println("-------------------------------------");
				outputFilePath = output_file.getPath();
				//直接生成swf
				String genFilePath = outputFilePath.substring(0,outputFilePath.lastIndexOf("."))+".swf";
				Pdf2Swf swf_tool = new Pdf2Swf("/home/appuser/swftools/src/pdf2swf", outputFilePath, genFilePath);
				swf_tool.start();
			} catch (ConnectException e) {
				e.printStackTrace();
			}
		}
	}
	
	private class Pdf2Swf extends Thread{
		private String swfCmd,outFile,fileName;
		public Pdf2Swf(String swfCmd,String outFile,String fileName) {
			super();
			this.swfCmd = swfCmd;
			this.outFile = outFile;
			this.fileName = fileName;
		}
		
		Process process = null;
		
		private boolean isWindowsSystem() {
			String p = System.getProperty("os.name");
			System.out.println("系统判断："+p.toLowerCase());
			return p.toLowerCase().contains("windows");
		}

		@Override
		public void run() {
			super.run();
			try {
				if(isWindowsSystem()){
					// 如果是windows系统
					// 命令行命令
					String cmd = swfCmd + " \"" + outFile + "\" -s flashversion=9 -o \"" + fileName + "\"";
					// Runtime执行后返回创建的进程对象
					process = Runtime.getRuntime().exec(cmd);
				}else{
					System.out.println("转换SWF文件");
					System.out.println("-------------------------------------");
					System.out.println("原路径："+swfCmd);
					System.out.println("新路径："+outFile);
					System.out.println("-------------------------------------");
					// 如果是linux系统,路径不能有空格，而且一定不能用双引号，否则无法创建进程
					String[] cmd = new String[6];
					cmd[0] = swfCmd;
					cmd[1] = outFile;
					cmd[2] = "-s";
					cmd[3] = "flashversion=9";
					cmd[4] = "-o";
					cmd[5] = fileName;
					// Runtime执行后返回创建的进程对象
					process = Runtime.getRuntime().exec(cmd);
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			try {
				new DoOutput(process.getInputStream()).start();
				new DoOutput(process.getErrorStream()).start();
				// 调用waitFor方法，是为了阻塞当前进程，直到cmd执行完
				process.waitFor();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		private class DoOutput extends Thread {
			public InputStream is;

			public DoOutput(InputStream is) {
				this.is = is;
			}
			public void run() {
				BufferedReader br = new BufferedReader(new InputStreamReader(
						this.is));
				String str = null;
				try {
					// 这里并没有对流的内容进行处理，只是读了一遍
					while ((str = br.readLine()) != null)
						;
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					if (br != null) {
						try {
							br.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		
	}
}
